package com.example.lib.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class TimeUtils {
    public static final String yyyyMMddHHmmssSSSSS = "yyyy-MM-dd HH:mm:ss.SSSSSS";

    public static final String yyyyMMddHHmmss = "yyyy-MM-dd HH:mm:ss";

    public static final long OneHour = 1000 * 60 * 60;
    public static final long OneDay = OneHour * 24;
    public static final long OneWeek = OneDay * 7;

    public static String timeToMin(int seconds) {
        return (seconds / 60) + "";
    }

    public static int timeToMinInt(int seconds) {
        return (seconds / 60);
    }

    public static int timeToHour(int seconds) {
        return seconds / 3600;
    }

    public static Long timeToHourMillSecend(long seconds) {
        return (seconds / 1000) / 3600;
    }

    public static int timeToSeconds(int seconds) {
        return seconds % 60;
    }

    public static String formatHmTimeString(int seconds) {
        return timeToMin(seconds);
    }

    /**
     * 格式化毫秒数为 12h231min212这样的时间格式。
     *
     * @param seconds 秒数
     * @return 格式化后的字符串
     */
    public static String formatMillisecond(int seconds) {
        if (seconds == 0L) return "";
        //        int seconds = (int) ms;
        int finalSec = seconds % 60;
        int finalMin = seconds / 60 % 60;
        int finalHour = (seconds % 86400) / 3600;
        int day = seconds / 86400;

        StringBuilder msBuilder = new StringBuilder();
        if (day > 0) {
            msBuilder.append(day).append("天");
        }

        if (finalHour > 0) {
            msBuilder.append(finalHour).append("时");
        }

        if (finalMin > 0) {
            msBuilder.append(finalMin).append("分");
        } else {
            msBuilder.append("0分");
        }

        if (finalSec > 0) {
            msBuilder.append(finalSec).append("秒");
        } else {
            msBuilder.append("0秒");
        }

        return msBuilder.toString();
    }

    /**
     * 格式化毫秒数为 xx:xx:xx这样的时间格式。
     *
     * @param ms 毫秒数
     * @return 格式化后的字符串
     */
    public static String formatMs(long ms) {
        if (ms == 0L) return "00:00:00";
        int seconds = (int) (ms / 1000);
        //        int seconds = (int) ms;
        int finalSec = seconds % 60;
        int finalMin = seconds / 60 % 60;
        int finalHour = seconds / 3600;

        StringBuilder msBuilder = new StringBuilder("");
        if (finalHour > 9) {
            msBuilder.append(finalHour).append(":");
        } else if (finalHour > 0) {
            msBuilder.append("0").append(finalHour).append(":");
        } else {
            msBuilder.append("00").append(":");
        }

        if (finalMin > 9) {
            msBuilder.append(finalMin).append(":");
        } else if (finalMin > 0) {
            msBuilder.append("0").append(finalMin).append(":");
        } else {
            msBuilder.append("00").append(":");
        }

        if (finalSec > 9) {
            msBuilder.append(finalSec);
        } else if (finalSec > 0) {
            msBuilder.append("0").append(finalSec);
        } else {
            msBuilder.append("00");
        }

        return msBuilder.toString();
    }

    public static String timeToMonth(long time) {
        SimpleDateFormat format = new SimpleDateFormat("MMM", Locale.getDefault());
        return format.format(new Date(time));
    }

    public static String timeToMonth(long time, String f) {
        SimpleDateFormat format = new SimpleDateFormat(f, Locale.getDefault());
        return format.format(new Date(time));
    }

    public static String timeToSportDetails(long time) {
        SimpleDateFormat format = new SimpleDateFormat("dd, yyyy, HH:mm", Locale.getDefault());
        return format.format(new Date(time));
    }

    public static String formatTime(long time, String format) {
        return new SimpleDateFormat(format, Locale.getDefault()).format(new Date(time));
    }

    public static String formatTime(long time, String format, Locale locale) {
        return new SimpleDateFormat(format, locale).format(new Date(time));
    }

    /**
     * 将时间戳格式化成HH:mm样式
     *
     * @param time 时间错
     * @return 格式化时间
     */
    public static String formatHmTime(long time) {
        SimpleDateFormat format = new SimpleDateFormat("HH:mm", Locale.getDefault());
        return format.format(new Date(time));
    }

    /**
     * 将时间戳格式化成HH:mm样式
     *
     * @param time 时间错
     * @return 格式化时间
     */
    public static String formatMdHmTime(long time) {
        SimpleDateFormat format = new SimpleDateFormat("MM.dd HH:mm", Locale.getDefault());
        return format.format(new Date(time));
    }


    /**
     * 将时间戳格式化成yyyy.MM.dd HH:mm样式
     *
     * @param time 时间错
     * @return 格式化时间
     */
    public static String formatYmdHmTime(long time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd HH:mm", Locale.getDefault());
        return format.format(new Date(time));
    }

    /**
     * 将时间戳格式化成HH:mm:ss样式
     *
     * @param time 时间错
     * @return 格式化时间
     */
    public static String formatHmsTime(long time) {
        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss", Locale.getDefault());
        return format.format(new Date(time));
    }


    /**
     * 将时间戳格式化成yyyy.MM.dd HH:mm:ss样式
     *
     * @param time 时间错
     * @return 格式化时间
     */
    public static String formatYmdHmsTime(long time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.getDefault());
        return format.format(new Date(time));
    }

    /**
     * 将时间戳格式化成yyyy.MM.dd HH:mm:ss样式
     *
     * @param time 时间错
     * @return 格式化时间
     */
    public static String formatYmd(long time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd", Locale.getDefault());
        return format.format(new Date(time));
    }


    /**
     * 将时间戳格式化成MM.dd样式
     *
     * @param time 时间错
     * @return 格式化时间
     */
    public static String formatMd(long time) {
        SimpleDateFormat format = new SimpleDateFormat("MM.dd", Locale.getDefault());
        return format.format(new Date(time));
    }

    //转换时间
    public static Long timeToLong(String time, String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format, Locale.getDefault());
        try {
            Date parse = simpleDateFormat.parse(time);
            if (parse != null) {
                return parse.getTime();
            } else return 0L;
        } catch (ParseException e) {
            return 0L;
        }
    }
}
